package cn.fighter3.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.fighter3.entity.node.*;
import cn.fighter3.service.impl.BookService;
import cn.fighter3.service.impl.PeopleService;
import cn.fighter3.service.node.*;
import org.neo4j.driver.*;

//import org.neo4j.driver.v1.*;
import org.neo4j.driver.types.Node;
import org.neo4j.driver.types.Path;
import org.neo4j.driver.types.Relationship;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/test")
public class testController {
    @Autowired
    PeopleService peopleService;

    @Autowired
    BookService bookService;

    @Autowired
    CnbookDao cnbookDao;
    @Autowired
    FgbookDao fgbookDao;
    @Autowired
    MscbookDao mscbookDao;
    @Autowired
    MulbookDao mulbookDao;
    @Autowired
    PapbookDao papbookDao;
    @Autowired
    PerbookDao perbookDao;
    @Autowired
    PeopleDao peopleDao;

    Driver driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "neo4j123"));
    private Session session=driver.session();

    @GetMapping("/{cql}")
    public cn.fighter3.result.Result findbycql(@PathVariable("cql") String cql) throws Exception {
        List<CustomNode> customNodes=new ArrayList<>();
        try{
            //String cql1 = "match (n:People) return n limit 1";
            //match (n:Cnbook) -[r:Authorof]->(p:People) return n,p limit 3
            //CREATE(n:People {authorName:'TEST-NAME', authorId:'007'}) return n
            //match (n:People) where n.authorId='007' delete n   删除返回result为空
            String cql1=cql;
            String cql2=cql1.replace('<','[');
            String cql3=cql2.replace('&',']');
            System.out.println("cyther语句："+cql3);

            Result result = session.run(cql3);
            Map<Long,String> nodeIds=new HashMap<Long, String>();
            Map<Long,String> relationIds=new HashMap<Long, String>();
            List<Integer> numlist=new ArrayList<>();
            int flag=0;
            int i=0;

            while (result.hasNext()){
                Record record=result.next();
                List<Value> values=record.values();
                for (Value value:values){
                    //System.out.println("typeeeeee");
                    i++;
//                    System.out.println(i);
//                    System.out.println(value.type().name());

                    if("NODE".equals(value.type().name())) {
                        Node node = value.asNode();
                        System.out.println(node.labels().toString());
                        if (!nodeIds.containsKey(node.id())) {
                            nodeIds.put(node.id(),"");
                        //System.out.println("nameeeee");
                            if (node.labels().toString().equals("[People]")) {
                                String authorid = node.get("authorId").asString();
                                People people=peopleService.findByAuthorId(authorid).get();
                                CustomNode customNode = new CustomNode(node.get("authorName").asString(), 1, people,0);//peopleDao.countpeo(people.getAuthorId())
                                customNodes.add(customNode);
                                //System.out.println(node.labels().toString());
//                                System.out.println(node.get("authorId").asString());
//                                System.out.println(node.get("authorName").asString());
                            } else if (node.labels().toString().equals("[Cnbook]") || node.labels().toString().equals("[Cnbook, Book]")) {
                                String bookid = node.get("bookId").asString();
                                Cnbook cnbook= bookService.findByBookId1(bookid);
                                CustomNode customNode = new CustomNode(node.get("title").asString(), 2, cnbook,0);//cnbookDao.countcn(cnbook.getBookId())
                                customNodes.add(customNode);
                            } else if (node.labels().toString().equals("[Fgbook]") || node.labels().toString().equals("[Fgbook, Book]")) {
                                String bookid = node.get("bookId").asString();
                                Fgbook fgbook=bookService.findByBookId2(bookid);
                                CustomNode customNode = new CustomNode(node.get("title").asString(), 3, fgbook,0);//fgbookDao.countfg(fgbook.getBookId())
                                customNodes.add(customNode);
                                System.out.println(node.labels().toString());
                            } else if (node.labels().toString().equals("[Mscbook]") || node.labels().toString().equals("[Mscbook, Book]")) {
                                String bookid = node.get("bookId").asString();
                                Mscbook mscbook=bookService.findByBookId3(bookid);
                                CustomNode customNode = new CustomNode(node.get("title").asString(), 4, mscbook,0);//mscbookDao.countmsc(mscbook.getBookId())
                                customNodes.add(customNode);
                            } else if (node.labels().toString().equals("[Mulbook]") || node.labels().toString().equals("[Mulbook, Book]")) {
                                String bookid = node.get("bookId").asString();
                                Mulbook mulbook=bookService.findByBookId4(bookid);
                                CustomNode customNode = new CustomNode(node.get("title").asString(), 5, mulbook,0);//mulbookDao.countmul(mulbook.getBookId())
                                customNodes.add(customNode);
                            } else if (node.labels().toString().equals("[Papbook]") || node.labels().toString().equals("[Papbook, Book]")) {
                                String bookid = node.get("bookId").asString();
                                Papbook papbook=bookService.findByBookId5(bookid);
                                CustomNode customNode = new CustomNode(node.get("title").asString(), 6, papbook,0);//papbookDao.countpap(papbook.getBookId())
                                customNodes.add(customNode);
                            } else if (node.labels().toString().equals("[Perbook]") || node.labels().toString().equals("[Perbook, Book]")) {
                                String bookid = node.get("bookId").asString();
                                Perbook perbook=bookService.findByBookId6(bookid);
                                CustomNode customNode = new CustomNode(node.get("title").asString(), 7, perbook,0);//perbookDao.countper(perbook.getBookId())
                                customNodes.add(customNode);
                            }

                    }

                    }
                    else if("STRING".equals(value.type().name())){
                        String res=value.asString();
                        CustomNode customNode = new CustomNode(res, 1, null,0);//perbookDao.countper(perbook.getBookId())
                        customNodes.add(customNode);
                    }
                    else if("INTEGER".equals(value.type().name())){
                        flag=1;
                        Integer num=value.asInt();
                        numlist.add(num);
                    }
                    else if("RELATIONSHIP".equals(value.type().name())){
                        Relationship relationship=value.asRelationship();
                        Map<String,Object> map=relationship.asMap();
                        //relationship.
                        System.out.println(relationship.startNodeId());
                        System.out.println(relationship.endNodeId());
                        System.out.println(relationship.type());
                        System.out.println(map);
                    }
                    else if("PATH".equals(value.type().name())){
                        Path path=value.asPath();
                        Iterable<Node> nodes=path.nodes();
                        for(Node node:nodes){
                            System.out.println(node.id());
                            System.out.println(node.values());
                            System.out.println(node.labels());
                        }
                        Iterable<Relationship> relationships=path.relationships();
                        for (Relationship relationship:relationships){
                            Map<String,Object> map=relationship.asMap();
                            System.out.println(relationship.startNodeId());
                            System.out.println(relationship.endNodeId());
                            System.out.println(relationship.type());
                        }
                    }

                }

            }

            if(numlist.size()!=0 && flag==1){
                CustomNode customNode=new CustomNode("numlist",0,numlist,0);
                customNodes.add(customNode);
            }

//            int j=0;
//            for(j=0;j<customNodes.size();j++){
//                CustomNode customNode=customNodes.get(j);
//                System.out.println(customNode.getId());
//            }
            //System.out.println(result);
            //System.out.println(result.single().get( 0 ));
        } catch (Exception e) {
            System.out.println("error");
            System.out.println(e.getClass() + "," + e.getMessage());
        }
        return new cn.fighter3.result.Result(200,"hello",customNodes);
//        Object object = result;
//        return object;

    }



    @GetMapping("/cql1/{cql}")
    public cn.fighter3.result.Result findbycql1(@PathVariable("cql") String cql) throws Exception {
        List<CustomNode> customNodes=new ArrayList<>();
        try{
            //String cql1 = "match (n:People) return n limit 1";
            //match (n:Cnbook) -[r:Authorof]->(p:People) return n,p limit 3
            //CREATE(n:People {authorName:'TEST-NAME', authorId:'007'}) return n
            //match (n:People) where n.authorId='007' delete n   删除返回result为空
            String cql1=cql;
            String cql2=cql1.replace('<','[');
            String cql3=cql2.replace('&',']');
            System.out.println("cyther语句："+cql3);

            Result result = session.run(cql3);
            Map<Long,String> nodeIds=new HashMap<Long, String>();
            Map<Long,String> relationIds=new HashMap<Long, String>();

            while (result.hasNext()){
                Record record=result.next();
                List<Value> values=record.values();
                for (Value value:values){
                    System.out.println("typeeeeee");
                    System.out.println(value.type().name());
                    if("NODE".equals(value.type().name())) {
                        Node node = value.asNode();
                        if (!nodeIds.containsKey(node.id())) {
                            nodeIds.put(node.id(),"");
                            //System.out.println("nameeeee");
                            if (node.labels().toString().equals("[People]")) {
                                String authorid = node.get("authorId").asString();
                                People people=peopleService.findByAuthorId(authorid).get();
                                CustomNode customNode = new CustomNode(node.get("authorName").asString(), 1, people,0);//peopleDao.countpeo(people.getAuthorId())
                                customNodes.add(customNode);
                                //System.out.println(node.get("authorId").asString());
                                //System.out.println(node.get("authorName").asString());
                            } else if (node.labels().toString().equals("[Cnbook]")) {
                                String bookid = node.get("bookId").asString();
                                Cnbook cnbook= bookService.findByBookId1(bookid);
                                CustomNode customNode = new CustomNode(node.get("title").asString(), 2, cnbook,0);//cnbookDao.countcn(cnbook.getBookId())
                                customNodes.add(customNode);
                            } else if (node.labels().toString().equals("[Fgbook]")) {
                                String bookid = node.get("bookId").asString();
                                Fgbook fgbook=bookService.findByBookId2(bookid);
                                CustomNode customNode = new CustomNode(node.get("title").asString(), 3, fgbook,0);//fgbookDao.countfg(fgbook.getBookId())
                                customNodes.add(customNode);
                            } else if (node.labels().toString().equals("[Mscbook]")) {
                                String bookid = node.get("bookId").asString();
                                Mscbook mscbook=bookService.findByBookId3(bookid);
                                CustomNode customNode = new CustomNode(node.get("title").asString(), 4, mscbook,0);//mscbookDao.countmsc(mscbook.getBookId())
                                customNodes.add(customNode);
                            } else if (node.labels().toString().equals("[Mulbook]")) {
                                String bookid = node.get("bookId").asString();
                                Mulbook mulbook=bookService.findByBookId4(bookid);
                                CustomNode customNode = new CustomNode(node.get("title").asString(), 5, mulbook,0);//mulbookDao.countmul(mulbook.getBookId())
                                customNodes.add(customNode);
                            } else if (node.labels().toString().equals("[Papbook]")) {
                                String bookid = node.get("bookId").asString();
                                Papbook papbook=bookService.findByBookId5(bookid);
                                CustomNode customNode = new CustomNode(node.get("title").asString(), 6, papbook,0);//papbookDao.countpap(papbook.getBookId())
                                customNodes.add(customNode);
                            } else if (node.labels().toString().equals("[Perbook]")) {
                                String bookid = node.get("bookId").asString();
                                Perbook perbook=bookService.findByBookId6(bookid);
                                CustomNode customNode = new CustomNode(node.get("title").asString(), 7, perbook,0);//perbookDao.countper(perbook.getBookId())
                                customNodes.add(customNode);
                            }

                        }

                    }
                    else if("RELATIONSHIP".equals(value.type().name())){
                        Relationship relationship=value.asRelationship();
                        Map<String,Object> map=relationship.asMap();
                        //relationship.
                        System.out.println(relationship.startNodeId());
                        System.out.println(relationship.endNodeId());
                        System.out.println(relationship.type());
                        System.out.println(map);
                    }
                    else if("PATH".equals(value.type().name())){
                        Path path=value.asPath();
                        Iterable<Node> nodes=path.nodes();
                        for(Node node:nodes){
                            System.out.println(node.id());
                            System.out.println(node.values());
                            System.out.println(node.labels());
                        }
                        Iterable<Relationship> relationships=path.relationships();
                        for (Relationship relationship:relationships){
                            Map<String,Object> map=relationship.asMap();
                            System.out.println(relationship.startNodeId());
                            System.out.println(relationship.endNodeId());
                            System.out.println(relationship.type());
                        }
                    }

                }

            }


            //System.out.println(result);
            //System.out.println(result.single().get( 0 ));
        } catch (Exception e) {
            System.out.println("error");
            System.out.println(e.getClass() + "," + e.getMessage());
        }
        return new cn.fighter3.result.Result(200,"hello",customNodes);
//        Object object = result;
//        return object;

    }




}
